package com.wurd.config;

import com.wurd.entity.Test;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class HibernateTemplateConfig {
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
    @Value("${hibernate.database-platform}")
    private String hibernateDatabasePlatform;
    @Value("${hibernate.ddl-auto}")
    private String hibernateDDLAuto;
    @Value("${hibernate.database}")
    private String hibernateDatabase;
    @Value("${hibernate.show-sql}")
    private String hibernateShowSql;
    @Autowired
    private DataSource dataSource;

    @Bean
    public SessionFactory sessionFactory() {
        org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
        Properties properties = getHibernateFullProperties();
        configuration.setProperties(properties);
//        configuration.addPackage("com.wurd.entity");
        configuration.addAnnotatedClass(Test.class);
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;
    }

    private Properties getHibernateFullProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.connection.url", jdbcUrl);
        properties.setProperty("hibernate.connection.username", username);
        properties.setProperty("hibernate.connection.password", password);
        properties.setProperty("hibernate.connection.driver_class", driverClassName);
        properties.setProperty("hibernate.show_sql", hibernateShowSql);
        properties.setProperty("hibernate.hbm2ddl.auto", hibernateDDLAuto);
        properties.setProperty("hibernate.dialect", hibernateDatabasePlatform);
        properties.setProperty("hibernate.database", hibernateDatabase);
        return properties;
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", hibernateShowSql);
        properties.setProperty("hibernate.hbm2ddl.auto", hibernateDDLAuto);
        properties.setProperty("hibernate.dialect", hibernateDatabasePlatform);
        properties.setProperty("hibernate.database", hibernateDatabase);
        return properties;
    }

    @Bean
    public LocalSessionFactoryBean localSessionFactory() {
        AnnotationSessionFactoryBean localSessionFactory = new AnnotationSessionFactoryBean();
        localSessionFactory.setDataSource(dataSource);
        Properties properties = getHibernateProperties();
        localSessionFactory.setHibernateProperties(properties);
        localSessionFactory.setAnnotatedClasses(new Class[]{
                Test.class
        });
        return localSessionFactory;
    }

    @Bean
    public HibernateTransactionManager hibernateTransactionManager(LocalSessionFactoryBean localSessionFactory) {
        HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
        hibernateTransactionManager.setSessionFactory(localSessionFactory.getObject());
        return hibernateTransactionManager;
    }

    @Bean
    public HibernateTemplate hibernateTemplate(LocalSessionFactoryBean localSessionFactory) {
        HibernateTemplate hibernateTemplate = new HibernateTemplate();
        hibernateTemplate.setSessionFactory(localSessionFactory.getObject());
        return hibernateTemplate;
    }

}
